
package com.fx.apartment.modules.dynamic.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fx.apartment.common.base.BaseServiceImpl;
import com.fx.apartment.modules.dynamic.entity.Comment;
import com.fx.apartment.modules.dynamic.entity.Dynamic;
import com.fx.apartment.modules.dynamic.mapper.DynamicMapper;
import com.fx.apartment.modules.dynamic.service.IDynamicService;
import com.fx.apartment.modules.dynamic.vo.CommentVO;
import com.fx.apartment.modules.dynamic.mapper.CommentMapper;
import com.fx.apartment.modules.dynamic.service.ICommentService;
import com.fx.apartment.common.tool.utils.BeanUtil;
import com.fx.apartment.common.tool.utils.Func;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 朋友圈评论 服务实现类
 *
 * @author QinXueFeng
 * @since 2021-04-19
 */
@Service
@AllArgsConstructor
public class CommentServiceImpl extends BaseServiceImpl<CommentMapper, Comment> implements ICommentService {

	private IDynamicService dynamicService;

	private DynamicMapper dynamicMapper;


	@Override
	public List<CommentVO> unreadMessages() {
		//TODO 当前用户ID
		List<Dynamic> list = dynamicService.list(Wrappers.<Dynamic>lambdaQuery().eq(Dynamic::getCreateUser, ""));
		List<Long> longList = list.stream().map(Dynamic::getId).collect(Collectors.toList());
		List<Comment> commentList = list(Wrappers.<Comment>lambdaQuery().eq(Comment::getStatus, 0).eq(Comment::getReplyUserId, "")
				.or(w -> w.in(Comment::getDynamicId, longList).eq(Comment::getReleaseStatus, 0)));
		List<Long> longList1 = commentList.stream().map(Comment::getId).collect(Collectors.toList());
		List<Dynamic> dynamicList = dynamicService.listByIds(longList1);
		Map<Long, String> map = dynamicList.stream().collect(Collectors.toMap(Dynamic::getId, Dynamic::getResourcesUrl));
		List<CommentVO> CommentVOList = Lists.newArrayListWithCapacity(commentList.size());
		for (Comment comment : commentList) {
			CommentVO commentVO = BeanUtil.copy(comment, CommentVO.class);
			commentVO.setUrl(map.get(comment.getDynamicId()));
		}
		return CommentVOList;
	}

	@Override
	public boolean fetchMessages(String ids) {
		List<Long> longList = Func.toLongList(ids);
		//TODO 当前用户ID
		boolean update1 = update(Wrappers.<Comment>lambdaUpdate().set(Comment::getStatus, 1).eq(Comment::getReplyUserId, "").in(Comment::getId,longList));
		boolean update2 = update(Wrappers.<Comment>lambdaUpdate().set(Comment::getReleaseStatus, 1).ne(Comment::getReplyUserId, "").in(Comment::getId,longList));
		return update1&&update2;
	}

	@Override
	public boolean deleteByIds(String ids) {
		List<Comment> list = listByIds(Func.toLongList(ids));
		Map<Long, List<Comment>> map = list.stream().collect(Collectors.groupingBy(Comment::getDynamicId));
		map.forEach((k,v)->{
			dynamicMapper.updateCommentNum(k,-v.size());
		});
		return deleteLogic(Func.toLongList(ids));
	}

}
